-- 独完技能
local duwan = fk.CreateSkill{
    name = "steam__duwan",
}
Fk:loadTranslationTable{
  ["steam__duwan"] = "独完",
  [":steam__duwan"] = "当你造成或受到伤害后，你可以展示对方（为其他角色）与你各一半手牌（向上取整），然后可以选择一项：①与其各摸共计展示【杀】数牌(至多为4张)；②与其交换各自展示的点数唯一牌。",
  ["#steam__duwan-choice"] = "独完：请选择一项",
  ["duwan_draw"] = "与其各摸共计展示【杀】数牌",
  ["duwan_exchange"] = "与其交换各自展示的点数唯一牌",
}
-- 独完触发技
-- 造成伤害后触发
duwan:addEffect(fk.Damage, {
    anim_type = "control",
    can_trigger = function(self, event, target, player, data)
        -- 当造成伤害后触发
        return target==player and player:hasSkill(self.name) and data.to and data.to~=player
        and not player:isKongcheng() and not data.to:isKongcheng()
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        
        -- 确定对方角色（为其他角色）
        local other_player =data.to 
        -- 计算各一半手牌数（向上取整）
        local player_hand_count = #player:getCardIds("h")
        local other_hand_count = #other_player:getCardIds("h")
        local player_show_count = math.ceil(player_hand_count / 2)
        local other_show_count = math.ceil(other_hand_count / 2)
        
        -- 让玩家选择要展示的手牌
        local player_show_cards = {}
        if player_show_count > 0 then
            player_show_cards = room:askForCard(player, player_show_count, player_show_count, false, self.name, false, ".|.|.|hand")
            if not player_show_cards or #player_show_cards == 0 then
                return
            end
        end
        
        -- 让其他玩家选择要展示的手牌
        local other_show_cards = {}
        if other_show_count > 0 then
            other_show_cards = room:askForCardsChosen(player,other_player, other_show_count, other_show_count,"h", self.name)
            if not other_show_cards or #other_show_cards == 0 then
                return
            end
        end
        
        -- 展示手牌
        if #player_show_cards > 0 then
            player:showCards(player_show_cards)
        end
        if #other_show_cards > 0 then
            other_player:showCards(other_show_cards)
        end
        
        -- 统计展示的【杀】数量
        local sha_count = 0
        for _, card_id in ipairs(player_show_cards) do
            local card = Fk:getCardById(card_id)
            if card and card.trueName == "slash" then
                sha_count = sha_count + 1
            end
        end
        for _, card_id in ipairs(other_show_cards) do
            local card = Fk:getCardById(card_id)
            if card and card.trueName == "slash" then
                sha_count = sha_count + 1
            end
        end
        
        -- 找出点数唯一的牌
        local player_unique_cards = {}
        local other_unique_cards = {}
        local all_numbers = {}
        
        -- 统计所有展示牌的点数
        for _, card_id in ipairs(player_show_cards) do
            local card = Fk:getCardById(card_id)
            if card then
                local number = card.number
                all_numbers[number] = (all_numbers[number] or 0) + 1
            end
        end
        for _, card_id in ipairs(other_show_cards) do
            local card = Fk:getCardById(card_id)
            if card then
                local number = card.number
                all_numbers[number] = (all_numbers[number] or 0) + 1
            end
        end
        
        -- 找出点数唯一的牌
        for _, card_id in ipairs(player_show_cards) do
            local card = Fk:getCardById(card_id)
            if card and all_numbers[card.number] == 1 then
                table.insert(player_unique_cards, card_id)
            end
        end
        for _, card_id in ipairs(other_show_cards) do
            local card = Fk:getCardById(card_id)
            if card and all_numbers[card.number] == 1 then
                table.insert(other_unique_cards, card_id)
            end
        end
        
        -- 提供选择
        local choices = {}
        if sha_count > 0 then
            table.insert(choices, "duwan_draw")
        end
        if #player_unique_cards > 0 and #other_unique_cards > 0 then
            table.insert(choices, "duwan_exchange")
        end
        
        if #choices == 0 then return end
        
        local choice = room:askForChoice(player, choices, self.name, "#steam__duwan-choice")
        
        if choice == "duwan_draw" then
            -- 与其各摸共计展示【杀】数牌
            player:drawCards(math.min(sha_count,4), self.name)
            other_player:drawCards(math.min(sha_count,4), self.name)
        elseif choice == "duwan_exchange" then
            -- 与其交换各自展示的点数唯一牌
            if #player_unique_cards > 0 and #other_unique_cards > 0 then
                room:moveCards({
                    ids = player_unique_cards,
                    from = player,
                    to = other_player,
                    toArea = Card.PlayerHand,
                    moveReason = fk.ReasonExchange,
                    skillName = self.name,
                })
                room:moveCards({
                    ids = other_unique_cards,
                    from = other_player,
                    to = player,
                    toArea = Card.PlayerHand,
                    moveReason = fk.ReasonExchange,
                    skillName = self.name,
                })
            end
        end
    end,
})

-- 受到伤害后触发
duwan:addEffect(fk.Damaged, {
    anim_type = "control",
    can_trigger = function(self, event, target, player, data)
        -- 当造成伤害后触发
        return target==player and player:hasSkill(self.name) and data.from and data.from~=player
        and not player:isKongcheng() and not data.from:isKongcheng()
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        
        -- 确定对方角色（为其他角色）
        local other_player =data.from
        -- 计算各一半手牌数（向上取整）
        local player_hand_count = #player:getCardIds("h")
        local other_hand_count = #other_player:getCardIds("h")
        local player_show_count = math.ceil(player_hand_count / 2)
        local other_show_count = math.ceil(other_hand_count / 2)
        
        -- 让玩家选择要展示的手牌
        local player_show_cards = {}
        if player_show_count > 0 then
            player_show_cards = room:askForCard(player, player_show_count, player_show_count, false, self.name, false, ".|.|.|hand")
            if not player_show_cards or #player_show_cards == 0 then
                return
            end
        end
        
        -- 让其他玩家选择要展示的手牌
        local other_show_cards = {}
        if other_show_count > 0 then
            other_show_cards = room:askForCardsChosen(player,other_player, other_show_count, other_show_count,"h", self.name)
            if not other_show_cards or #other_show_cards == 0 then
                return
            end
        end
        
        -- 展示手牌
        if #player_show_cards > 0 then
            player:showCards(player_show_cards)
        end
        if #other_show_cards > 0 then
            other_player:showCards(other_show_cards)
        end
        
        -- 统计展示的【杀】数量
        local sha_count = 0
        for _, card_id in ipairs(player_show_cards) do
            local card = Fk:getCardById(card_id)
            if card and card.trueName == "slash" then
                sha_count = sha_count + 1
            end
        end
        for _, card_id in ipairs(other_show_cards) do
            local card = Fk:getCardById(card_id)
            if card and card.trueName == "slash" then
                sha_count = sha_count + 1
            end
        end
        
        -- 找出点数唯一的牌
        local player_unique_cards = {}
        local other_unique_cards = {}
        local all_numbers = {}
        
        -- 统计所有展示牌的点数
        for _, card_id in ipairs(player_show_cards) do
            local card = Fk:getCardById(card_id)
            if card then
                local number = card.number
                all_numbers[number] = (all_numbers[number] or 0) + 1
            end
        end
        for _, card_id in ipairs(other_show_cards) do
            local card = Fk:getCardById(card_id)
            if card then
                local number = card.number
                all_numbers[number] = (all_numbers[number] or 0) + 1
            end
        end
        
        -- 找出点数唯一的牌
        for _, card_id in ipairs(player_show_cards) do
            local card = Fk:getCardById(card_id)
            if card and all_numbers[card.number] == 1 then
                table.insert(player_unique_cards, card_id)
            end
        end
        for _, card_id in ipairs(other_show_cards) do
            local card = Fk:getCardById(card_id)
            if card and all_numbers[card.number] == 1 then
                table.insert(other_unique_cards, card_id)
            end
        end
        
        -- 提供选择
        local choices = {}
        if sha_count > 0 then
            table.insert(choices, "duwan_draw")
        end
        if #player_unique_cards > 0 and #other_unique_cards > 0 then
            table.insert(choices, "duwan_exchange")
        end
        
        if #choices == 0 then return end
        
        local choice = room:askForChoice(player, choices, self.name, "#steam__duwan-choice")
        
        if choice == "duwan_draw" then
            -- 与其各摸共计展示【杀】数牌
            player:drawCards(sha_count, self.name)
            other_player:drawCards(sha_count, self.name)
        elseif choice == "duwan_exchange" then
            -- 与其交换各自展示的点数唯一牌
            if #player_unique_cards > 0 and #other_unique_cards > 0 then
                room:moveCards({
                    ids = player_unique_cards,
                    from = player,
                    to = other_player,
                    toArea = Card.PlayerHand,
                    moveReason = fk.ReasonExchange,
                    skillName = self.name,
                })
                room:moveCards({
                    ids = other_unique_cards,
                    from = other_player,
                    to = player,
                    toArea = Card.PlayerHand,
                    moveReason = fk.ReasonExchange,
                    skillName = self.name,
                })
            end
        end
    end,
})

return duwan 